<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-page-header content="聊天详情" @back="$router.back()" />
        </el-card>
        <el-card class="!border-none content" shadow="never">
            <div class="chat_window">
                <div class="top_menu">
                    <div class="title">对话框</div>
                    <el-link type="primary" :underline="false" class="top-btn" @click="onUpdata">刷新</el-link>
                </div>
                <ul class="messages" v-if="list">
                    <li v-for="(item,index) in list" :key="index" class="message appeared" :class="[item.position == 2 ?'right':'left']">
                        <div class="avatar" v-if="item.position != 2">
                            <img :src="item.from_userinfo.face" alt="">
                            <div class="avatar-txt">{{ item.from_userinfo.realname }}</div>
                        </div>
                        <div class="text_wrapper">
                            <div class="txt">{{ item.msg_type == 'TIMTextElem' ? item.msg_content: item.type_name }}</div>
                        </div>
                        <div class="avatar" v-if="item.position == 2">
                            <img :src="item.from_userinfo.face" alt="">
                            <div class="avatar-txt">{{ item.from_userinfo.realname }}</div>
                        </div>
                    </li>
                </ul>
                <div class="select">
                    <el-select
                        v-model="choose"
                        placeholder="切换身份"
                        style="width: 180px"
                    >
                        <el-option :label="info.from_userinfo.realname" :value="0"/>
                        <el-option :label="info.to_userinfo.realname" :value="1"/>
                    </el-select>
                    <span>使用当前身份发言</span>
                </div>
                <div class="bottom">
                    <div class="w-[600px]">
                        <el-input v-model="chart_txt" placeholder="请输入..." />
                    </div>
                    <el-button class="bottom-btn" @click="onClick_send">发送</el-button>
                </div>
                
            </div>
        </el-card>
    </div>
</template>
<script lang="ts" setup name="groupChartDetail">
    import { msgDetail, addMsg } from '@/api/group';
    const route = useRoute()
    import feedback from '@/utils/feedback'
    const list:any = ref([{
        position: '',
        from_userinfo: {
            realname: '',
            face: ''
        },
        to_userinfo: {
            realname: ''
        }
    }])
    // 回复的信息
    const chart_txt = ref('')
    const choose = ref(0)

    const onUpdata = () => {
        getDetails();
        feedback.msgSuccess('刷新成功')
    }
    // 发送按钮
    const onClick_send = async () => {
        console.log(choose.value)
        console.log(chart_txt.value)
        console.log(info)
        let params = {
            content: '',
            from: '',
            to: ''
        }
        if (chart_txt.value != '') {
            params.content = chart_txt.value;
            if (choose.value == 0) {
                params.from = info.from_userinfo.user_str;
                params.to = info.to_userinfo.user_str;
            } else {
                params.from = info.to_userinfo.user_str;
                params.to = info.from_userinfo.user_str;
            }
            const res = await addMsg(params)
            feedback.msgSuccess('发送成功');
            chart_txt.value = '';
            setTimeout(()=>{
                getDetails()
            },500)
        }
    }
    let stop = true;
    const info = reactive({
        from_userinfo: {
            realname: '',
            user_str: ''
        },
        to_userinfo: {
            realname: '',
            user_str: ''
        }
    })
    const getDetails = async () => {
        const data = await msgDetail({
            from: route.query.from,
            from_type: route.query.from_type,
            to: route.query.to,
            to_type: route.query.to_type
        })
        list.value = data.list;
        console.log(stop+'stop')
        if (stop) {
            stop = false;
            info.from_userinfo = data.list[0].from_userinfo;
            info.to_userinfo = data.list[0].to_userinfo;
        } 
    }
    if (route.query.from) {
        getDetails()
    }
</script>
<style scoped lang="scss">
.content{
    position: relative;
    height: 750px;
    padding: 20px 0;
    background-color: #64a6ff;
}
.chat_window{
    position: absolute;
    width: calc(100% - 20px);
    max-width: 800px;
    height: 720px;
    border-radius: 10px;
    background-color: #fff;
    left: 50%;
    top: 50%;
    transform: translateX(-50%) translateY(-50%);
    box-shadow: 0 10px 20px rgba(0, 0, 0, 0.15);
    background-color: #f8f8f8;
    overflow: hidden;
}
.top_menu{
    position: relative;
    background-color: #fff;
    width: 100%;
    padding: 10px 0;
    height: 40px;
    box-shadow: 0 1px 30px rgba(0, 0, 0, 0.1);
    .title{
        text-align: center;
        color: #333;
        font-size: 14px;
    }
    .top-btn{
        position: absolute;
        top: 10px;
        right: 20px;

    }
}
.messages{
    position: relative;
    list-style: none;
    padding: 20px 10px 0 10px;
    margin: 0;
    height: 565px;
    overflow: hidden;
    overflow-y: scroll;
    .message {
        display: flex;
        clear: both;
        overflow: hidden;
        padding-bottom: 20px;
        transition: all 0.5slinear;
        opacity: 0;
        .avatar{
            position: relative;
            width: 75px;
            height: 50px;
            border-radius: 50%;
            display: inline-block;
            img{
                width: 50px;
                height: 50px;
                border-radius: 50%;
            }
            .avatar-txt{
                width: 75px;
                font-size: 12px;
                white-space: wrap;
                // white-space: nowrap;
                // overflow: hidden;
                // text-overflow: ellipsis;
            }
        }
    }
    .appeared{
        opacity: 1;
        min-height: 67px;
    }
    .left{
        .avatar{
            // background-color: #f5886e;
            // background-size: 100%;
            // float: left;
        }
        .text_wrapper{
            background-color: #ffe6cb;
            margin-left: 20px;
            display: inline-block;
            padding: 20px;
            border-radius: 6px;
            width: calc(100% - 105px);
            min-width: 100px;
            position: relative
        }
        .text_wrapper::before {
            content: '';
            position: absolute;
            left: -14px;
            top: 50%;
            transform: translateY(-50%);
            border-top: 16px solid transparent;
            border-bottom: 16px solid transparent;
            border-right: 18px solid #ffe6cb;
        }
        .txt{
            font-size: 18px;
            font-weight: 300;
            color: #c48843;
        }
    }
    .right{
        .avatar{
            // background-color: #fdbf68;
            // float: right;
        }
        .text_wrapper{
            background-color: #c7eafc;
            margin-right: 20px;
            display: inline-block;
            padding: 20px;
            border-radius: 6px;
            width: calc(100% - 105px);
            min-width: 100px;
            position: relative;
            float: right;
        }
        .text_wrapper::after {
            content: '';
            position: absolute;
            right: -14px;
            top: 50%;
            transform: translateY(-50%);
            border-top: 16px solid transparent;
            border-bottom: 16px solid transparent;
            border-left: 18px solid #c7eafc;
        }
        .txt{
            font-size: 18px;
            font-weight: 300;
            color: #c48843;
        }
    }
}
.select{
    padding-left: 20px;
    padding-top: 20px;
    background: #FFFFFF;
}
.bottom{
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #FFFFFF;
    padding: 20px;
    padding-top: 0;
}
.bottom-btn{
    width: 140px;
    height: 45px;
    display: inline-block;
    border-radius: 10px;
    background-color: #64a6ff;
    border: 2px solid #64a6ff;
    background-image: linear-gradient(90deg, #2a71ff, #64a6ff);
    color: #fff;
    cursor: pointer;
    transition: all 0.2slinear;
    text-align: center;
    font-size: 18px;
    font-weight: 300;
    display: inline-block;
}

</style>